Miles Sound System SDK 7.2a

Q:

How do I smoothly ramp up or ramp down the volume of a sample over time?

A:

There is a special digital filter for this very purpose. To use it, you attach the filter to the sample, and then just set the time and ramp levels that you want to use. To find the filter and attach it to the HSAMPLE, you would use code like this:


HPROVIDER volume_ramp;
if ( AIL_find_filter( "Volume Ramp Filter", &volume_ramp ) )
{
AIL_set_sample_processor( S, SP_FILTER_0, volume_ramp );
}

Once you've done this, the sample can be volume-ramped whenever you like. You set the level once with the ramp time at zero to set the starting ramp level, and then again with a non-zero ramp time to set the final level and begin the ramping. Remember to always set the ramp time first, and then the ramp level. So, to smoothly ramp the volume up from 0.0 to 1.0, use something like this:


// First, set the ramp time to zero, to init the ramp level
F32 ramp_time = 0.0f;
AIL_sample_stage_property( S, SP_FILTER_0, "Ramp Time", 0, &ramp_time, 0 );
//
// we want start at level zero
F32 ramp_level = 0.0f;
AIL_sample_stage_property( S, SP_FILTER_0, "Ramp To", 0, &ramp_level, 0 );
//
//
//ok now, set the ramp time to 1000 ms (fade in over 1 second)
ramp_time = 1000.0f;
AIL_sample_stage_property( S, SP_FILTER_0, "Ramp Time", 0, &ramp_time, 0 );
//
// ramp up to 1.0 (full volume)
F32 ramp_level = 1.0f;
AIL_sample_stage_property( S, SP_FILTER_0, "Ramp To", 0, &ramp_level, 0 );

Similarly, to fade out over one second, do something like this:


// First, set the ramp time to zero, to init the ramp level
F32 ramp_time = 0.0f;
AIL_sample_stage_property( S, SP_FILTER_0, "Ramp Time", 0, &ramp_time, 0 );
//
// we want start at full volume
F32 ramp_level = 1.0f;
AIL_sample_stage_property( S, SP_FILTER_0, "Ramp To", 0, &ramp_level, 0 );
//
//
//ok now, set the ramp time to 1000 ms (fade out over 1 second)
ramp_time = 1000.0f;
AIL_sample_stage_property( S, SP_FILTER_0, "Ramp Time", 0, &ramp_time, 0 );
//
// ramp down to 0.0 (silence)
F32 ramp_level = 0.0f;
AIL_sample_stage_property( S, SP_FILTER_0, "Ramp To", 0, &ramp_level, 0 );

Note that the filter remembers the last ramp level, so you only have to set the starting ramp level if you don't already know what it is. So, if you ramp from 0.0 to 1.0, and then later want to ramp back down to 0.0, you don't need to set the initial ramp level to 1.0.

Next Topic (How do I play a 3D digital sound?)

Previous Topic (How do I start an audio file somewhere in the middle?)


Group: FAQs and How Tos
Related Sections: Volume Ramp Filter

For technical support, e-mail Miles3@radgametools.com
© Copyright 1991-2007 RAD Game Tools, Inc. All Rights Reserved.